package com.koron.css2.clientCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.clientCenter.queryBean.UserInfoMergeDetailQuery;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.stereotype.Service;
import org.swan.bean.MessageBean;

import java.util.List;

/**
 * 根据客户编号查询对应的账户编号
 *
 * @author 朱超
 * @since 2021-12-27 15:15:08
 */
@Service
public class GetAccountByCtm implements ServerInterface {
    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            UserInfoMapper userInfoMapper = factory.getMapper(UserInfoMapper.class);

            // 1. 根据请求json获取实例bean
            UserInfoMergeDetailQuery userInfoMergeDetailQuery = JsonUtils.objectToPojo(req.getData(),UserInfoMergeDetailQuery.class);
            String ctmNo = userInfoMergeDetailQuery.getCtmNo();
            String accountNo = userInfoMergeDetailQuery.getAccountNo();
            // 2. 根据ctmNo查询所属的accountNo
            List<String> accountNoList = userInfoMapper.getAccountNoListByCtmNo(ctmNo);
            // 2.1 如果原来的accountNo在列表中，就置顶
            if (accountNoList.contains(accountNo)) {
                accountNoList.add(0,accountNoList.remove(accountNo.indexOf(accountNo)));
            } // 2.2 如果不在，就添加到第一位
            else {
                accountNoList.add(0,accountNo);
            }
            MessageBean<List> msg = MessageBean.create(Constant.MESSAGE_INT_SUCCESS,"根据客户编号查询对应的账户编号成功",List.class);
            msg.setData(accountNoList);
            return msg;
        } catch (Exception e) {
            logger.error("根据客户编号查询对应的账户编号失败",e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL,"根据客户编号查询对应的账户编号失败",void.class);
        }
    }
}
